public class Solution917 {
    public String reverseOnlyLetters(String s) {
        char[] c=s.toCharArray();
        int i=0,j=s.length()-1;
        while (i<j){
            while (i<s.length() && !((c[i]>='a') && (c[i]<='z') || (c[i]>='A') && (c[i]<='Z'))){
                i++;
            }
            while (j>=0 && !((c[j]>='a') && (c[j]<='z') || (c[j]>='A') && (c[j]<='Z'))){
                j--;
            }
            if (i<j){
                char t=c[i];
                c[i]=c[j];
                c[j]=t;
                i++;
                j--;
            }
        }
        return String.valueOf(c);
    }

    public static void main(String[] args) {
        System.out.println(new Solution917().reverseOnlyLetters("a-bC-dEf-ghIj"));
    }
}
